//----------------------------------------------------------------------------
//
// TSDuck - The MPEG Transport Stream Toolkit
// Copyright (c) 2005-2025, Thierry Lelegard
// BSD-2-Clause license, see LICENSE.txt file or https://tsduck.io/license
//
//----------------------------------------------------------------------------
//
//  TSUnit test suite for class ts::Xoshiro256ss
//
//----------------------------------------------------------------------------

#include "tsXoshiro256ss.h"
#include "tsByteBlock.h"
#include "tsunit.h"


//----------------------------------------------------------------------------
// The test fixture
//----------------------------------------------------------------------------

class Xoshiro256ssTest: public tsunit::Test
{
    TSUNIT_DECLARE_TEST(Random);

private:
    void testRead(ts::RandomGenerator& prng, const ts::ByteBlock& expected);
};

TSUNIT_REGISTER(Xoshiro256ssTest);


//----------------------------------------------------------------------------
// Test cases
//----------------------------------------------------------------------------

void Xoshiro256ssTest::testRead(ts::RandomGenerator& prng, const ts::ByteBlock& expected)
{
    ts::ByteBlock generated;
    TSUNIT_ASSERT(prng.readByteBlock(generated, expected.size()));
    // debug() << "Xoshiro256ssTest: " << ts::UString::Dump(generated, ts::UString::SINGLE_LINE | ts::UString::C_STYLE) << std::endl;

    if (expected != generated) {
        debug() << "Xoshiro256ssTest: readByteBlock failed" << std::endl
                << "  Expected: " << ts::UString::Dump(expected, ts::UString::SINGLE_LINE) << std::endl
                << "  Returned: " << ts::UString::Dump(generated, ts::UString::SINGLE_LINE) << std::endl;
    }

    TSUNIT_EQUAL(expected.size(), generated.size());
    TSUNIT_ASSERT(expected == generated);
}

TSUNIT_DEFINE_TEST(Random)
{
    ts::Xoshiro256ss prng;

    static const uint8_t seed1[25] = {
        0xE7, 0xD9, 0x12, 0x04, 0xF0, 0x3E, 0xD3, 0x46, 0xF0, 0xA1, 0x4E, 0x44, 0xAD, 0x6F, 0x92, 0xAD,
        0x08, 0x63, 0x6B, 0xB5, 0xD3, 0xEC, 0xC9, 0xDE, 0x02,
    };
    static const uint8_t seed2[25] = {
        0x98, 0x0D, 0x18, 0x16, 0x8E, 0x78, 0xD0, 0x6E, 0xF0, 0xF6, 0x12, 0x9E, 0x18, 0x23, 0xB9, 0x41,
        0xC8, 0xFA, 0xA6, 0xB9, 0xF5, 0xD2, 0xE2, 0xD0, 0x8D,
    };

    TSUNIT_ASSERT(!prng.ready());
    TSUNIT_ASSERT(prng.seed(seed1, sizeof(seed1)));
    TSUNIT_ASSERT(!prng.ready());
    TSUNIT_ASSERT(prng.seed(seed2, sizeof(seed2)));
    TSUNIT_ASSERT(prng.ready());

    testRead(prng, {
        0x6F, 0xB8, 0x99,
    });
    testRead(prng, {
        0x35, 0xEC, 0x7E, 0x9D, 0x1F, 0xF2, 0x2B, 0x6C, 0x6D, 0x80, 0xAD, 0xAB,
    });
    testRead(prng, {
        0xA6, 0xDF, 0xBA, 0x28, 0x80, 0x93, 0x2C, 0x3B, 0x49, 0x9D, 0x69, 0x9B, 0x78, 0x1F, 0x98, 0x89,
        0x98, 0x9E, 0xE8, 0x99, 0x1F, 0x3B, 0xA0, 0x9A, 0x8B, 0x9B, 0x80, 0xE3, 0x36, 0x9C, 0x5D, 0xC1,
        0xBF, 0x82, 0x54, 0x8A, 0x7C, 0x2A, 0x88, 0xE7, 0x6E, 0x42, 0x7C, 0xC1, 0x92,
    });

    static const uint8_t seed4[64] = {
        0xF7, 0x0E, 0xBA, 0x98, 0x56, 0xD4, 0x30, 0xDD, 0xE3, 0x82, 0xAF, 0xFF, 0x74, 0x0D, 0x44, 0xAD,
        0x3F, 0xC2, 0xCC, 0x4C, 0x8E, 0x7F, 0xF4, 0x71, 0xD9, 0xCD, 0x83, 0x57, 0x93, 0x8C, 0xD4, 0x5F,
        0x2E, 0x3A, 0x75, 0x69, 0x52, 0xC8, 0x54, 0x3F, 0x84, 0xFB, 0x10, 0x96, 0xCF, 0x07, 0x97, 0xE4,
        0x8F, 0x93, 0x48, 0xC1, 0x71, 0x15, 0x74, 0x77, 0x70, 0x57, 0xE6, 0x5D, 0xD3, 0x18, 0x27, 0xD7,
    };

    static const uint8_t seed5[32] = {
        0x92, 0x75, 0x36, 0x65, 0xCC, 0x3E, 0x24, 0x04, 0xAF, 0x88, 0x2D, 0x4E, 0x86, 0xCE, 0x81, 0x53,
        0x19, 0x8A, 0xF3, 0xAB, 0x98, 0x4E, 0x1E, 0xB3, 0x23, 0xC2, 0xF5, 0x2E, 0x38, 0x5B, 0x71, 0x95,
    };

    prng.reset();
    TSUNIT_ASSERT(!prng.ready());
    TSUNIT_ASSERT(prng.seed(seed4, sizeof(seed4)));
    TSUNIT_ASSERT(prng.ready());

    testRead(prng, {
        0x13, 0x1C, 0x1B,
    });
    testRead(prng, {
        0x27, 0x42, 0xB8, 0xFB, 0x76, 0xC3, 0xBD, 0x28, 0x5B, 0xC5, 0x1C, 0x29, 0x31, 0x21, 0x34, 0x54,
        0x87, 0xEA, 0x5F, 0x32, 0x74, 0x1D, 0xFD, 0x40, 0x06, 0xEF, 0x4D, 0x9F, 0x33, 0x07, 0x45, 0xD6,
        0x54, 0x0F, 0x4A, 0x0E, 0x14, 0x46, 0x9B, 0x8A, 0x40, 0x06, 0x23, 0x90, 0x99, 0xA5, 0x55, 0x39,
    });

    prng.reset();
    TSUNIT_ASSERT(!prng.ready());
    TSUNIT_ASSERT(prng.seed(seed4, sizeof(seed4)));
    TSUNIT_ASSERT(prng.ready());

    testRead(prng, {
        0x13, 0x1C, 0x1B,
    });
    TSUNIT_ASSERT(prng.ready());
    TSUNIT_ASSERT(prng.seed(seed5, sizeof(seed5)));
    TSUNIT_ASSERT(prng.ready());
    testRead(prng, {
        0x50, 0x64, 0x83, 0x00, 0xDF, 0xCD, 0xA6, 0xE8, 0x13, 0xAC, 0x78, 0x6E, 0x54, 0x80, 0x45, 0x80,
        0x8E, 0x0F, 0xA5, 0xDC, 0x76, 0xE9, 0x71, 0xE8, 0xFF, 0xF2, 0xDB, 0x13, 0xE4, 0xFB, 0x6F, 0xD4,
        0xC3, 0x54, 0xF8, 0x96, 0x87, 0x5C, 0xE6, 0x2E, 0x12, 0x68, 0x5E, 0x74, 0x6A, 0x93, 0x2D, 0x2A,
    });
}
